﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using Com.CMS263.Util;

namespace Com.CMS263.Web.manage.ManageArtType
{
    public partial class EditExtAttribute : System.Web.UI.Page
    {
        public ExtAttribute ext = new ExtAttribute();
        public ExtAttributeBLL extbll = new ExtAttributeBLL();
        int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Convert.ToInt32(Request["id"]);
            if (!IsPostBack)
            {
                Bind();
            }
        }
        public void Bind()
        {
            ext = (ExtAttribute)extbll.FindModelById("ExtAttribute", id);
            txtAttrName.Text = ext.attrName;
            txtAttrTxtName.Text = ext.attrTxtName;
            txtOrderNum.Text = ext.orderNum.ToString ();
            ddlAttrType.SelectedValue = ext.attrType.ToString ();
            txtAttrValue.Text = ext.attrValue;
            defaultValue.Text = ext.defaultValue;
            hdfTypeId.Value = ext.typeId.ToString ();
            hdfId.Value = ext.id.ToString();
            if (ext.notNull == 1)
            {
                chkNotNull.Checked = true;
            }
            else
            {
                chkNotNull.Checked = false;
            }
            if (ext.showInBack == 1)
            {
                chkShowInBack.Checked = true;
            }
            else
            {
                chkShowInBack.Checked = false;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ExtAttribute extat = new ExtAttribute();
            string attrname = txtAttrName.Text;
            string sttrtxtname = txtAttrTxtName.Text;
            string orderNum = txtOrderNum.Text;
            string attrtype = ddlAttrType.SelectedValue;
            string defaultvalue = defaultValue.Text;
            string attrvalue = txtAttrValue.Text;
            string typeid = hdfTypeId.Value;
            string id=hdfId .Value; 
            int notnull = 0;
            int showinback = 0;
            if (chkNotNull.Checked)
            {
                notnull = 1;
            }
            else
            {
                notnull = 0;
            }
            if (chkShowInBack.Checked)
            {
                showinback = 1;
            }
            else
            {
                showinback = 0;
            }
            if (string.IsNullOrEmpty(attrname))
            {
                StringUtil.Alert("属性名不能为空", "-1");
            }
            else
            {
                attrname = StringUtil.checkStr(attrname);
            }
            if (string.IsNullOrEmpty(sttrtxtname))
            {
                StringUtil.Alert("属性中文名不能为空", "-1");
            }
            else
            {
                sttrtxtname = StringUtil.checkStr(sttrtxtname);
            }
            if (string.IsNullOrEmpty(orderNum))
            {
                StringUtil.Alert("排序编号不能为空", "-1");
            }
            else
            {
                orderNum = StringUtil.checkStr(orderNum);
            }
            extat.id = Convert.ToInt32(id);
            extat.attrName = attrname;
            extat.attrTxtName = sttrtxtname;
            extat.attrType = Convert.ToInt32(attrtype);
            extat.defaultValue = defaultvalue;
            extat.notNull = notnull;
            extat.attrValue = attrvalue;
            extat.orderNum = Convert.ToInt32(orderNum);
            extat.showInBack = showinback;
            extat.typeId = Convert.ToInt32(typeid);
            extbll.entity = extat;
            bool m = extbll.UpDate();
            if (m)
            {
                StringUtil.Alert("修改成功！", "/manage/ManageArtType/AddExtAttribute.aspx?id="+typeid);
            }
            else
            {
                StringUtil.Alert("修改失败请从新添加！", "-1");
            }

        }
    }
}
